﻿
// SerialAssistantDlg.h: 头文件
//

#include "SerialPort.h"
#include "Digistatic.h"
#include "ChartCtrl/ChartCtrl.h"
#include "ChartCtrl/ChartLineSerie.h"

#pragma once
#define LineLen 200

typedef struct
{
	int counter;
	char CS;
	int Gx;
	int Gy;
	int Gz;
	int Ax;
	int Ay;
	int Az;
	int Wx;
	int Wy;
	int Wz;
	int Fx;
	int Fy;
	int Fz;
	float wx;
	float wy;
	float wz;
	float fx;
	float fy;
	float fz;
} IMUData;

typedef struct
{
	float time;		// UTC时间
	double Lat;		// 纬度
	char NS;		// 南北半球
	double Lng;		// 经度
	char WE;		// 东西半球
	short state;	// GPS解类型
	short SatNum;	// 卫星数
	float HDOP;		// HDOP
	float Alt;		// 海拔高度
	float Herr;		// 大地水准面高度异常值
	float xx;		// 差分数据期限
	short BaseNum;	// 参考站标号
	short chk;		// 校验
} GPGGAPKG;

//char SS[4][32];		// SOL_STATUS

typedef struct
{
	char header[256];
	char SOL_STATUS[64];
	UCHAR SS;
	char VEL_TYPE[64];
	UCHAR VT;
	float latency;
	float age;
	double vel_h;
	double heading;
	double vel_v;
	float xx;
	char CRC[16];
}BESTVELPKG;

class MyListCtrl : public CListCtrl
{
public:
	int nextRowNUm = 0;
	int idx = 0;
	CString nextContent;

	void AddIMUItem(IMUData imudata);
	void AddGPSItem(GPGGAPKG gpgga, BESTVELPKG bestvel);
};

// CSerialAssistantDlg 对话框
class CSerialAssistantDlg : public CDialogEx
{
// 构造
public:

	CSerialAssistantDlg(CWnd* pParent = nullptr);	// 标准构造函数

// 对话框数据
#ifdef AFX_DESIGN_TIME
	enum { IDD = IDD_SERIALASSISTANT_DIALOG };
#endif


// 实现
protected:
	HICON m_hIcon;

	// 生成的消息映射函数
	virtual BOOL OnInitDialog();
	afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
	afx_msg void OnPaint();
	afx_msg HCURSOR OnQueryDragIcon();
	afx_msg LRESULT OnSerialMsg(WPARAM wParam, LPARAM lParam);
	afx_msg LRESULT OnDataErrMsg(WPARAM wParam, LPARAM lParam);
	DECLARE_MESSAGE_MAP()
public:
	afx_msg void OnSelchangeComboCom();
	afx_msg void OnSelchangeComboBdr();
	afx_msg void OnSelchangeComboCheckbit();
	afx_msg void OnSelchangeComboStopbit();
	afx_msg void OnClickedBtnOpenser();
	afx_msg void OnClickedBtnExit();
	virtual void DoDataExchange(CDataExchange* pDX);

	// 数据异常标志
	UINT msg_errlost = 101;
	UINT msg_errchkfaile = 102;

	CString time_tmp = CString();
	SYSTEMTIME st = { 0 };		//系统时间
	int secCounter = 0;
	int rcvCounter;
	unsigned int SYSCNT = 0;
	BOOL counterPause = FALSE;
	CComboBox ComB_com, ComB_bdr, ComB_stopbit, ComB_checkbit;
	CComboBox Comb_com_g;
	CComboBox Comb_bdr_g;
	CComboBox Comb_stp_g;
	CComboBox Comb_chk_g;
	CDigiStatic Timer_day;
	CDigiStatic Timer_hour;
	CDigiStatic Timer_minute;
	CDigiStatic Timer_month;
	CDigiStatic Timer_second;
	CDigiStatic Timer_year;
	CDigiStatic EditCounter;
	CButton BtnPause;
	CButton BtnOpen;
	CButton BtnExit;
	MyListCtrl ListData;
	MyListCtrl ListDataGPS;

	CChartCtrl m_Chart_gyro, m_Chart_acc;
	CChartLineSerie* pLS_gx, * pLS_gy, * pLS_gz, * pLS_ax, * pLS_ay, * pLS_az;
	COLORREF rgb_RED = RGB(255,0,0);
	COLORREF rgb_BLUE = RGB(0,0,255);
	COLORREF rgb_GREEN = RGB(0,255,0);
	double tk = 0;
	double data_t[1000], data_gx[1000], data_gy[1000], data_gz[1000], data_ax[1000], data_ay[1000], data_az[1000];
	void addSample(double tk, IMUData imudata);

	int ListRowNum;
	IMUData imudata;			// 当前imu输出
	GPGGAPKG gpgga;
	BESTVELPKG bestvel;
	BOOL GPGGAOK, BESTVELOK;
	SerialPort mySerial;
	HANDLE TP_CommThread, GPS_CommThread;
	unsigned char pkgbuffer[64];	// 数据包存储
	char pkgbufferG0[512];	// 数据包存储
	char pkgbufferG1[512];	// 数据包存储

	FILE* dataFile;				// 解析数据文件
	FILE* ComDataFile;			// 串口数据文件
	FILE* GPSFile;				// GPS文件
	BOOL isRunningMsg;

	BOOL RcvChk();
	afx_msg void OnTimer(UINT_PTR nIDEvent);
	void SetLocalTime();
	afx_msg void OnClickedBtnPause();
	afx_msg void OnClickedBtnSetzero();
	void SerialParaInit();

	// 线程相关
	DWORD exitCode;		// exitCode读取成功标志
	static LPVOID threadPara;
	static BOOL isRunningIMU;
	static BOOL isRunningGPS;
	static DWORD WINAPI SerialThread(LPVOID lpara);
	static DWORD WINAPI SerialThreadGPS(LPVOID lpara);
	afx_msg void OnAppAbout();
	afx_msg void OnCbnSelchangeComboComG();
	afx_msg void OnCbnSelchangeComboBdrG();
	afx_msg void OnCbnSelchangeComboStopbitG();
	afx_msg void OnCbnSelchangeComboCheckbitG();
	
	CEdit editLog;
	CButton checkGPGGA;
	afx_msg void OnBnClickedCheckGpgga();
};

